package project.taip.wawwe.db;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.*;

import org.hibernate.Session;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

import project.taip.wawwe.model.*;
import project.taip.wawwe.util.HibernateHelper;
public class InitialDB {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Session sess = HibernateHelper.getInstance().openSession();
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder;
		try {
			docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse (new File("recipes_db.xml"));
	        doc.getDocumentElement ().normalize ();
            NodeList recipes = doc.getElementsByTagName("RECIPE");
            sess.beginTransaction();
            for(int s=0; s<recipes.getLength() ; s++){
                Element recipeNode = (Element)recipes.item(s);
                Element idNode = (Element) recipeNode.getElementsByTagName("ID").item(0); 
                Element titleNode = (Element) recipeNode.getElementsByTagName("TI").item(0); 
                Element servingsNode = (Element) recipeNode.getElementsByTagName("R").item(0); 
                Element descriptionNode = (Element) recipeNode.getElementsByTagName("PR").item(0); 
                Element imgNode = (Element) recipeNode.getElementsByTagName("IMG").item(0); 
                Element originNode = (Element) recipeNode.getElementsByTagName("ORIGIN").item(0); 
                String originTypeAtt = originNode.getAttribute("TYPE"); 
                Recipe recipe = new Recipe();
                recipe.setId(Long.parseLong(idNode.getTextContent()));
                recipe.setName(titleNode.getTextContent());
                recipe.setServings(Integer.parseInt(servingsNode.getTextContent()));
                recipe.setImage(imgNode.getTextContent());
                recipe.setOrigin(originNode.getTextContent());
                recipe.setOriginType(originTypeAtt);
                recipe.setDescription(descriptionNode.getTextContent());
                NodeList ingredientsList = recipeNode.getElementsByTagName("IN");
                for(int i=0; i<ingredientsList.getLength() ; i++){
                    Element inNode = (Element)ingredientsList.item(i);
                    Element qNode = (Element) inNode.getElementsByTagName("Q").item(0); 
                    Element unitNode = (Element) inNode.getElementsByTagName("UNIT").item(0); 
                    Element nameNode = (Element) inNode.getElementsByTagName("NAME").item(0); 
                    Ingredient ingredient = new Ingredient();
                    if (nameNode.getTextContent() != null && nameNode.getTextContent().length()>0)
                    	ingredient.setName(nameNode.getTextContent());
                    if (qNode.getTextContent() != null && qNode.getTextContent().length()>0)
                        	ingredient.setQuantity(Double.parseDouble(qNode.getTextContent()));
                    if (unitNode.getTextContent() != null && unitNode.getTextContent().length()>0)
                        	ingredient.setUnit(unitNode.getTextContent());
                    sess.save(ingredient);
                    recipe.addIngredients(ingredient);
                }
                sess.save(recipe);
            }
            sess.getTransaction().commit();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        finally {
        	sess.close();
        }

        

	}

}
